// pages/resume/personInfo/personInfo.js
Page({
  data: {
    headerPath:'../../../image/headerUpload.png',
    personInfoData:{},
    sexDefault:true,
    birthDefault:true,
    regionDefault: true,
    region:['上海市','上海市','徐汇区'],
    sex:['男','女'],
    index:0,
    date:'1990-09-01',
    wrongTip:"内容有误"
  },
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key:'index',
      success:function(res){
        that.setData({
          index:res.data
        })
      }
    })
    wx.getStorage({
      key: 'personInfoData',
      success: function (res) {
        console.log(that.data.personInfoData)
         that.setData({
           personInfoData: res.data,
           date:res.data.birth,
           region:res.data.region
         })
         console.log(that.data.personInfoData)
         if (that.data.personInfoData.sex) {
           that.setData({
             sexDefault: false
           })
         }
         if (that.data.personInfoData.birth) {
           that.setData({
             birthDefault: false
           })
         }
         if (that.data.personInfoData.region) {
           that.setData({
             regionDefault: false
           })
         }
      },
    })
  },
  headerUpload:function(){
    var that=this;
    wx.chooseImage({
      count:1,
      sizeType:['original','compressed'],
      sourceType:['album','camera'],
      success:function(res){
        that.setData({
          headerPath:res.tempFilePaths,
        })
      }
    })
  },
  bindSexChange: function (e) {
    this.setData({
      index: e.detail.value,
      sexDefault: false,
    })
  },
  bindBirthChange: function (e) {
    this.setData({
      date: e.detail.value,
      birthDefault: false,
    })
  },
  bindRegionChange: function (e) {
    var that=this;
    this.setData({
      region: e.detail.value,
      regionDefault: false,
    })
  },
  formSubmit:function(e){
    this.setData({
      personInfoData: e.detail.value,
    })
    var regName =/^([\u4e00-\u9fa5]){2,7}$/;
    var regEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    var regPhone = /^[1][3,4,5,7,8][0-9]{9}$/;
    if(!regName.test(this.data.personInfoData.name)){
      wx.showModal({
        title: '提示',
        content: '请输入正确的姓名',
        showCancel: false,
        success: function (res) {
        }
      })
    }else if(this.data.sexDefault==true){
      wx.showModal({
        title: '提示',
        content: '请选择您的性别',
        showCancel: false,
        success: function (res) {
        }
      })
    }else if(this.data.birthDefault==true){
      wx.showModal({
        title: '提示',
        content: '请选择您的出生日期',
        showCancel: false,
        success: function (res) {
        }
      })
    }else if(this.data.regionDefault==true){
      wx.showModal({
        title: '提示',
        content: '请选择您的现居住地',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (!regEmail.test(this.data.personInfoData.email)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的邮箱',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (!regPhone.test(this.data.personInfoData.phone)){
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号',
        showCancel: false,
        success: function (res) {
        }
      })
    }else{
      wx.setStorage({
        key: 'index',
        data: e.detail.value.sex,
        success: function (res) {
        }
      });
      this.data.personInfoData.sex = this.data.sex[e.detail.value.sex];
      var that=this;
      wx.request({
        url: "",
        data: that.data.personInfoData,
        header: { 'Content-Type': 'application/json' },
        success: function (res) {
        }
      });
      wx.setStorage({
        key: 'personInfoData',
        data: this.data.personInfoData,
        success: function (res) {
        }
      });
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        personInfo: currPage.data.personInfoData,
        personInfoShow: false,
        personInfoModify: true,
      });
      wx.showModal({
        title: '提示',
        content: '信息更新成功，请继续完善简历',
        showCancel: false,
        success: function (res) {
          wx.navigateBack()
        }
      })
    }
  },
})
function upload(page, path) {//图片上传，未完成
  wx.showToast({
    icon: "loading",
    title:"正在上传"
  }),
  wx.uploadFile({

  })
}